package com.teach;

/**
 * <pre>
 * - String <> 基本数据类型
 * Author : J.L.Zhou
 * E-Mail : 2233875735@qq.com
 * Tel : 151 1104 7708
 * Date : 2021-04-12 11:20
 * Version : 1.0
 * Copyright 2021 jlzhou.top Inc. All rights reserved.
 * Warning: this content is only for internal circulation of the company.
 *          It is forbidden to divulge it or use it for other commercial purposes.
 * </pre>
 **/
public class Demo02 {
  public static void main(String[] args) {
    //误区:
    int a = 1;
    long b = 2;
    String c = "c";
    b = a;//基本数据类型的隐式转换
    a = (int)b;//强转,基本数据类型的显示转换
//    a = (int)c;//强转,String是引用类型的,不存在类型的显示转换,只存在OOP中的向下造型()

    //String -> 基本数据类型
    // 构造方法
    String str1="11";
    int int1 = 0;
    int1 = new Integer(str1);
    System.out.println(int1);
    Long long1 = 0L;
    long1 = new Long(str1);
    System.out.println(long1);
    char char1 = '\u0000';
//    char1 = new Character(str1);//只有Character(char value);
    char1 = str1.charAt(0);
    char1 = str1.toCharArray()[0];
    boolean boolean1 = false;
    boolean1 = new Boolean("true");
    System.out.println(boolean1);
    boolean1 = new Boolean("trUe");
    System.out.println(boolean1);
    boolean1 = new Boolean("abc");
    System.out.println(boolean1);
    boolean1 = new Boolean("false");
    System.out.println(boolean1);
    boolean1 = new Boolean("");
    System.out.println(boolean1);
    boolean1 = new Boolean(null);
    System.out.println(boolean1);

    //static valueOf 推荐
    int1 = Integer.valueOf(str1);//Byte,Short,Integer,Long,Float,Double
    System.out.println(int1);
    long1 = Long.valueOf(str1,2);//Byte,Short,Integer,Long; radix:2~36
    System.out.println(long1);
//    char1 = Character.valueOf(str1);// valueOf(char c)
    boolean1 = Boolean.valueOf("true");//忽略大小写的TRUE->true,否则 -> false;
    System.out.println(boolean1);

    //static parseXXX
    int1 = Integer.parseInt(str1);
    System.out.println(int1);
    long1 = Long.parseLong(str1,36);
    System.out.println(long1);
    boolean1 = Boolean.parseBoolean("tRue");
    System.out.println(boolean1);

    //基本数据类型转字符串
//    str1 = int1; //错误
    //“”+xx 推荐
    str1 = ""+int1;
    System.out.println(str1);
    str1 = Long.toString(long1);
    System.out.println(str1);
    str1 = Long.toString(long1,2);// -> 2进制字符串
    System.out.println(str1);
    str1 = Long.toBinaryString(long1);// ->  2进制字符串
    str1 = Long.toOctalString(long1);// ->  8进制字符串
    str1 = Long.toHexString(long1);// -> 16进制字符串
    System.out.println(Integer.toString(-1,2));//-1
    System.out.println(Integer.toBinaryString(-1));//补码:11111111111111111111111111111111

  }
}
